<template>
  <div>
    <!-- 轮播图 -->
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="v in gallery" :key="v.id">
        <img :src="v.img_url" alt="" width="100%" />
      </van-swipe-item>
    </van-swipe>
    <!-- 30天无忧退货 -->
    <van-row>
      <van-col span="8">
        <van-icon name="checked" />
        <p>30天无忧退货</p>
      </van-col>
      <van-col span="8">
        <van-icon name="checked" />
        <p>48小时快速退货</p>
      </van-col>
      <van-col span="8">
        <van-icon name="checked" />
        <p>满88元免邮费</p>
      </van-col>
    </van-row>
    <!-- 描述  价格 -->
    <div class="description">
      <div class="one">{{ name }}</div>
      <div class="two">{{ goods_brief }}</div>
      <div class="third">¥ {{ retail_price }}.00 元</div>
    </div>
    <!-- 展示弹出层 -->
    <van-cell is-link @click="showPopup" style="margin-top: 0.02rem"
      >展示弹出层</van-cell
    >
    <van-popup
      v-model="show"
      position="bottom"
      :style="{ height: '40%' }"
      closeable
    >
      <!-- 弹出层 ==> 价格  库存 -->
      <div class="pro1">
        <img :src="info.list_pic_url" alt="" width="30%" />
        <p class="pice">价格：¥ {{ info.retail_price }}.00 元</p>
        <p class="cun">库存：{{ info.goods_number }}</p>
      </div>
      <!-- 弹出层 ==> 步进器 -->
      <div class="shu">
        <h1 style="margin-bottom: 0.25rem; margin-left: 0.25rem">数量：</h1>
        <van-stepper v-model="value" style="margin-left: 0.25rem" />
      </div>
    </van-popup>
    <!-- 产品参数 -->
    <div class="chanpin" style="margin-top: 0.2rem">
      <h2>产品参数</h2>
      <ul>
        <li v-for="v in attribute" :key="v.id">
          <span style="margin-right: 0.05rem; margin-left: 0.05rem;">{{
            v.name
          }}</span>
          <p style="overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        line-height: 25px;    
        max-height: 25px;
        -webkit-line-clamp: 1;
        -webkit-box-orient: vertical;">{{ v.value }}</p>
        </li>
      </ul>
    </div>
    <!-- 产品图  -->

    <div v-html="goods_desc" class="goods_desc"></div>

    <!-- 常见问题 -->
    <!-- 标题 -->
    <van-divider
      :style="{ color: '#000', borderColor: '#999', padding: '0 1rem' }"
    >
      <span style="font-size: 0.2rem">常见问题</span>
    </van-divider>
    <!-- 问题答案 -->
    <div class="question">
      <div v-for="v in issue" :key="v.id">
        <h3>{{ v.question }}</h3>
        <p>{{ v.answer }}</p>
      </div>
    </div>
    <!-- 相关产品 -->
    <!-- 标题 -->
    <van-divider
      :style="{ color: '#000', borderColor: '#999', padding: '0 1rem' }"
    >
      <span style="font-size: 0.2rem">相关产品</span>
    </van-divider>
    <div class="big_long">
      <ul>
        <li v-for="m in goodsList" :key="m.id" @click="tiao_self(m)">
          <img :src="m.list_pic_url" alt="" width="100%" />
          <h4>{{ m.name }}</h4>
          <p>{{ m.retail_price | fun_qian }}</p>
        </li>
      </ul>
    </div>
    <!-- 购物车 -->
    <van-goods-action>
      <van-goods-action-icon
        icon="star"
        v-if="iscollection == 1"
        text="已收藏"
        color="red"
        @click="iscollection = 0"
      />
      <van-goods-action-icon
        icon="star-o"
        v-if="iscollection == 0"
        text="未收藏"
        @click="iscollection = 1"
      />
      <!-- badge 右上角显示数量 -->
      <van-goods-action-icon
        icon="cart-o"
        text="购物车"
        :badge="num"
        @click="$router.push('/car')"
      />
      <van-goods-action-button
        type="danger"
        text="立即购买"
        @click="buyClick()"
      />
      <van-goods-action-button
        type="warning"
        text="加入购物车"
        @click="addCar()"
      />
    </van-goods-action>
  </div>
</template>

<script>
import { Toast } from "vant";
import {
  GetgoodsDetail,
  GetgoodsRelated,
  addCarData,
  GetGoodsCount,
} from "@/request/api";
export default {
  data() {
    return {
      num: "",
      iscollection: 0,
      value: 1,
      gallery: [],
      name: "",
      goods_brief: "",
      retail_price: "",
      show: false,
      info: [],
      attribute: "",
      goods_desc: "",
      issue: "",
      goodsList: [],
      //向购物车添加数据
      // 商品的id
      id: "",
      productId: "",
    };
  },
  methods: {
    tiao_self(m){
      this.$router.push('/goodsDetail/'+m.id)
      history.go(0)
    },
    showPopup() {
      this.show = true;
    },
    buyClick() {
      Toast.fail("功能未开放");
    },
    addCar() {
      // console.log(1111);
      this.show = !this.show;
      let token = localStorage.getItem("token");
      if (!this.show) {
        if (token) {
          this.num = this.value;
          addCarData({
            goodsId: this.id,
            productId: this.productId,
            number: this.value,
          }).then((res) => {
            if (res.errno == 0) {
              Toast.success("添加成功");
            }
          });
        } else {
          this.$router.push("/my");
        }
      }
    },
  },
  // 过滤器 处理金额
  filters: {
    fun_qian(v) {
      let num = v.toFixed(2);
      return "￥" + num + "元";
    },
  },
  components: {},
  created() {
    // console.log(this.$route.params);
    console.log(this.$route.params.id);
    // let id = this.$route.params.id;
    this.id = this.$route.params.id;
    GetgoodsDetail({ id: this.id }).then((res) => {
      // console.log(res);
      this.gallery = res.data.gallery;
      this.name = res.data.info.name;
      this.goods_brief = res.data.info.goods_brief;
      this.retail_price = res.data.info.retail_price;
      this.info = res.data.info;
      this.attribute = res.data.attribute;
      this.goods_desc = res.data.info.goods_desc;
      this.issue = res.data.issue;
      this.productId = res.data.productList[0].id;
    });
    GetgoodsRelated({ id: this.id }).then((res) => {
      this.goodsList = res.data.goodsList;
      console.log(this.goodsList);
    });
  },
};
</script>

<style lang="less" scoped>
// 30天无忧退货
.van-col {
  // background-color: lightblue;
  height: 0.28rem;
  line-height: 0.28rem;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 0.14rem;
  .van-icon {
    color: #8b0000;
  }
}
// 商品描述
.description {
  width: 100%;
  height: 1.5rem;
  background-color: #fff;
  overflow: hidden;
  .one {
    width: 100%;
    height: 0.4rem;
    // background-color: pink;
    text-align: center;
    line-height: 0.4rem;
    margin-top: 0.1rem;
  }
  .two {
    width: 100%;
    height: 0.4rem;
    // background-color: red;
    text-align: center;
    line-height: 0.4rem;
    margin-top: 0.05rem;
    font-size: 0.14rem;
  }
  .third {
    width: 100%;
    height: 0.4rem;
    // background-color: green;
    text-align: center;
    line-height: 0.4rem;
    color: #8b0000;
    margin-top: 0.05rem;
  }
}
// 弹出层
.pro1 {
  width: 100%;
  position: relative;
  p {
    font-size: 0.14rem;
    text-align: center;
  }
  .pice {
    position: absolute;
    top: 0.3rem;
    left: 1.3rem;
  }
  .cun {
    position: absolute;
    top: 0.8rem;
    left: 1.3rem;
  }
}
// 产品参数
.chanpin {
  width: 100%;
  min-height: 100%;
  background-color: #fff;
  box-sizing: border-box;
  padding: 0.1rem;
  margin-bottom: 0.2rem;
  h2 {
    margin-bottom: 0.1rem;
    margin-top: 0.16rem;
    font-size: 0.2rem;
  }
  ul {
    li {
      width: 100%;
      height: 0.3rem;
      display: flex;
      align-items: center;
      border-radius: 0.05rem;
      margin-bottom: 0.1rem;
      background-color: #efefef;
      span {
        font-size: 0.14rem;
        color: #999;
      }
      p {
        font-size: 0.14rem;
      }
    }
  }
}
// 深度选择器  针对商品详情图的遍历
/deep/.goods_desc {
  img {
    display: block;
    width: 100%;
  }
}
// 常见问题
.van-divider {
  background-color: #fff;
  margin: 0;
  height: 0.6rem;
}
.question {
  width: 100%;
  min-height: 100%;
  background-color: #fff;
  padding-bottom: 0.18rem;
  overflow: hidden;
  div {
    margin-top: 0.1rem;
    padding: 0 0.15rem;
    h3 {
      width: 100%;
      margin-bottom: 0.15rem;
      // font-weight:600;
    }
    p {
      width: 100%;
      font-size: 0.14rem;
      line-height: 0.18rem;
    }
  }
}
.big_long {
  background-color: #fff;
  width: 100%;
  min-height: 100%;
}
.big_long ul {
  width: 100%;
  min-height: 100%;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  flex-wrap: wrap;
}
.big_long ul li {
  width: 49%;
  min-height: 100%;
  margin-top: 10px;
}
.big_long ul li h4,
.big_long ul li p {
  width: 100%;
  text-align: center;
  margin-bottom: 10px;
}
.big_long ul li p {
  color: #8b0000;
}
.van-goods-action {
  z-index: 1000000000;
}
</style>
